# -*- coding: UTF-8 -*-
"""
@auth:buxiangjie
@date:2022/2/11 11:15
@describe: 
"""
import allure

from common.db_operate.db_operate import Saas
from common.common_func import Common
import re
import json


class Operate:

	def __init__(self, param, drop_down_box_content):
		self.param = param
		self.drop_down_box_content = drop_down_box_content

	def exec_func(self, env: str, func: str):
		n_func = func.split("@")[1]
		kwg = {
			"env": env
		}
		func_name = n_func.split("(")[0]
		re_func = re.search(r'[(](.*?)[)]', n_func).group()
		if len(re_func) > 2:
			n_re_func = re_func.strip("(").strip(")").split(",")
			for n in n_re_func:
				kwg[n.split("=")[0]] = n.split("=")[1]
		res = getattr(self, func_name)(**kwg)
		return res

	def get_credit_id(self, **kwargs) -> int:
		"""获取授信ID"""
		credit_id = Saas().get_credit_id(**kwargs)
		return credit_id

	def get_project_id(self, **kwargs) -> int:
		"""获取进件ID"""
		kwargs.update(self.param)
		project_id = Saas().get_project_id(**kwargs)
		return project_id

	def get_asset_id(self, **kwargs) -> int:
		"""获取资产ID"""
		if kwargs.get("types"):
			kwargs.update(self.param)
		asset_id = Saas().get_asset_id(**kwargs)
		return asset_id

	def get_amount_id(self, **kwargs) -> int:
		"""获取额度ID"""
		amount_id = Saas().get_amount_id(**kwargs)
		return amount_id

	def get_amount_detail_id(self, **kwargs) -> int:
		"""获取额度详情ID"""
		if kwargs.get("need_amount_id", None):
			kwargs["amount_id"] = self.param["amount_id"]
		amount_detail_id = Saas().get_amount_detail_id(**kwargs)
		return amount_detail_id

	def get_contract_confirm_id(self, **kwargs) -> list:
		"""获取合同确认列表ID"""
		contract_confirm_id = Saas().get_contract_confirm_id(**kwargs)
		return contract_confirm_id

	def get_contract_confirm_business_date(self, **kwargs) -> str:
		"""获取合同确认业务时间"""
		kwargs.update(self.param)
		business_date = Saas().get_contract_confirm_business_date(**kwargs)
		return business_date

	def get_source_project_id(self, **kwargs) -> str:
		"""获取来源进件ID"""
		kwargs.update(self.param)
		source_project_id = Saas().get_source_project_id(**kwargs)
		return source_project_id

	def get_contract_confirm_record_id(self, **kwargs) -> list:
		"""获取合同确认详情相关ID"""
		record_id = Saas().get_contract_confirm_record_id(**kwargs)
		return record_id

	def get_apply_time(self, **kwargs) -> str:
		"""获取进件申请时间"""
		kwargs.update(self.param)
		apply_time = Saas().get_apply_time(**kwargs)
		return apply_time

	def get_user_id(self, **kwargs) -> str:
		"""获取用户ID"""
		kwargs.update(self.param)
		user_id = Saas().get_user_id(**kwargs)
		return user_id

	def get_source_user_id(self, **kwargs) -> str:
		"""获取来源用户ID"""
		kwargs.update(self.param)
		source_user_id = Saas().get_source_user_id(**kwargs)
		return source_user_id

	def get_card_num(self, **kwargs) -> str:
		"""获取身份证号"""
		kwargs.update(self.param)
		card_num = Saas().get_card_num(**kwargs)
		return card_num

	def get_transaction_id(self, **kwargs) -> str:
		"""获取场景方ID"""
		kwargs.update(self.param)
		transaction_id = Saas().get_transaction_id(**kwargs)
		return transaction_id

	def get_log_id(self, **kwargs) -> str:
		"""获取日志ID"""
		if self.param:
			kwargs.update(self.param)
		log_id = Saas().get_log_id(**kwargs)
		return log_id

	def get_cash_detail(self, **kwargs) -> str:
		"""获取资金流水详情"""
		kwargs.update(self.param)
		cash_detail = Saas().get_cash_detail(**kwargs)
		return cash_detail

	@allure.step("减免前置处理")
	def relief(self, **kwargs) -> dict:
		"""减免前置处理"""
		asset_id = Saas().get_asset_id(**kwargs)
		api_info = Common.get_json_data("/config/mart_api_temp", f"query_repayment_plan.json")
		api_info["param"]["assetId"] = str(asset_id)
		repayment_plan: list = Common.response(
			addr=api_info["addr"],
			env=kwargs["env"],
			headers=api_info["headers"],
			method=api_info["method"],
			service="mart",
			param=api_info["param"]
		)["content"]
		relief_plan = []
		relief_amount_total = 0.0
		# 利息减免
		for plan in repayment_plan:
			if plan["repaymentPlanType"] == 2 and plan["repaymentStatus"] == 1 and float(plan["restAmount"])> 0:
				plan["reliefAmount"] = plan["restAmount"]
				plan["restAmountAfterReduction"] = plan["restAmount"]
				relief_amount_total += float(plan["restAmount"])
				relief_plan.append(plan)
				break
		# 费减免
		for plan in repayment_plan:
			if plan["repaymentPlanType"] > 2 and plan["repaymentStatus"] == 1 and float(plan["restAmount"])> 0:
				plan["reliefAmount"] = plan["restAmount"]
				plan["restAmountAfterReduction"] = plan["restAmount"]
				relief_amount_total += float(plan["restAmount"])
				relief_plan.append(plan)
				break
		relief_param = {
			"assetId": str(asset_id),
			"reliefPlanList": relief_plan,
			"remark": ""
		}
		if kwargs["types"] == "relief_confirm":
			relief_param["reliefAmountTotal"] = round(relief_amount_total, 2)
		return relief_param

	@allure.step("还款确认前置处理")
	def repayment(self, **kwargs):
		"""还款确认前置处理"""
		asset_id = Saas().get_asset_id(**kwargs)
		api_info = Common.get_json_data("/config/mart_api_temp", f"repayment_calculate.json")
		api_info["param"]["assetId"] = str(asset_id)
		api_info["param"]["repaymentTime"] = self.get_date()
		repayment_detail: dict = Common.response(
			addr=api_info["addr"],
			env=kwargs["env"],
			headers=api_info["headers"],
			method=api_info["method"],
			service="mart",
			param=api_info["param"]
		)["content"]
		repayment_detail["assetId"] = str(asset_id)
		repayment_detail["repaymentTime"] = api_info["param"]["repaymentTime"]
		repayment_detail["repayType"] = repayment_detail["repaymentType"]
		del repayment_detail["repaymentType"]
		repayment_detail["amountFrom"] = "2"
		repayment_detail["channel"] = "1"
		repayment_detail["remark"] = ""
		return repayment_detail

	def get_date(self, **kwargs) -> str:
		"""获取格式化后的当前时间"""
		return Common.get_date(**kwargs)

	def get_new_time(self, **kwargs) -> str:
		"""时间计算"""
		return Common.get_new_time(when=kwargs["when"], what=kwargs["what"], num=int(kwargs["num"])).split(" ")[0]
